﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bacon2015
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("************************************");
            Console.WriteLine("***** Bacon算法转换工具 v2.0.1 *****");
            Console.WriteLine("***** Copyright (c) CarlZhang  *****");
            Console.WriteLine("************************************");

            Console.WriteLine();

            string isContinue;
            do
            {
                string whichType;
                bool rightInput;
                do
                {
                    Console.WriteLine("请选择你要转换的内容：");
                    Console.WriteLine("1. 一堆未经转换的大小写字符串");
                    Console.WriteLine("2. 小写ab培根字符串");
                    Console.WriteLine("3. 大写AB培根字符串");
                    Console.WriteLine();

                    whichType = Console.ReadLine();

                    if (whichType != "1" && whichType != "2" && whichType != "3")
                    {
                        Console.WriteLine("输入值有误，请重新输入！");
                        rightInput = false;
                        Console.WriteLine();
                    }
                    else
                        rightInput = true;

                } while (!rightInput);

                Console.WriteLine();
                Console.WriteLine("请输入需要转换的字符串");
                string baconValue = Console.ReadLine();

                string newValue = GetInput(baconValue);

                string convertedValue = String2bacon(newValue, whichType);
                string finalValue = Bacon2Final(convertedValue, whichType);
                Console.WriteLine(finalValue);

                Console.WriteLine();
                Console.WriteLine("是否继续输入？y/n");

                isContinue = Console.ReadLine();
            } while (isContinue == "y" || isContinue == "Y");

            Console.ReadLine();
        }

        private static string GetInput(string input)
        {

            // 替换标点符号
            string returnValue = input.Replace(",", "");
            returnValue = returnValue.Replace(" ", "");
            returnValue = returnValue.Replace("'", "");
            returnValue = returnValue.Replace(".", "");
            returnValue = returnValue.Replace("_", "");
            returnValue = returnValue.Replace("-", "");
            returnValue = returnValue.Replace("+", "");
            returnValue = returnValue.Replace("/", "");
            returnValue = returnValue.Replace("*", "");
            returnValue = returnValue.Replace(":", "");

            return returnValue;
        }

        private static string String2bacon(string newValue, string selectType)
        {
            string trueBacon = string.Empty;
            if (newValue.Length % 5 == 0)
            {
                for (int i = 0; i < newValue.Length; i++)
                {
                    if (selectType == "1")
                    {
                        // 判断大小写字母，小写字母转换为A，大写字母转换为B
                        if (newValue[i] >= 'a' && newValue[i] <= 'z')
                            trueBacon += "A";
                        else if (newValue[i] >= 'A' && newValue[i] <= 'Z')
                            trueBacon += "B";
                    }
                    if (selectType == "2" || selectType == "3")
                        trueBacon += newValue[i];

                    // 每5个后面加个“-”，方便后续分割
                    if ((i + 1) % 5 == 0 && (i + 1) != newValue.Length)
                        trueBacon += "-";
                }
            }

            return trueBacon;
        }

        private static string Bacon2Final(string convertedValue, string selectType)
        {
            string returnValue = string.Empty;
            // 利用“-”进行分割
            string[] strValue = convertedValue.Split('-');


            // 将每个培根字符串转换为对应的字符
            foreach (var str in strValue)
            {
                if (selectType == "1" || selectType == "3")
                {
                    returnValue += SelectValue(str);
                }
                else if (selectType == "2")
                {
                    returnValue += SelectValueL(str);
                }
            }

            return returnValue;
        }

        /// <summary>
        /// 针对大写培根字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string SelectValue(string str)
        {
            string returnValue = string.Empty;
            switch (str)
            {
                case "AAAAA":
                    returnValue = "a";
                    break;
                case "AAAAB":
                    returnValue = "b";
                    break;
                case "AAABA":
                    returnValue = "c";
                    break;
                case "AAABB":
                    returnValue = "d";
                    break;
                case "AABAA":
                    returnValue = "e";
                    break;
                case "AABAB":
                    returnValue = "f";
                    break;
                case "AABBA":
                    returnValue = "g";
                    break;
                case "AABBB":
                    returnValue = "h";
                    break;
                case "ABAAA":
                    returnValue = "i/j";
                    break;
                case "ABAAB":
                    returnValue = "k";
                    break;
                case "ABABA":
                    returnValue = "l";
                    break;
                case "ABABB":
                    returnValue = "m";
                    break;
                case "ABBAA":
                    returnValue = "n";
                    break;
                case "ABBAB":
                    returnValue = "o";
                    break;
                case "ABBBA":
                    returnValue = "p";
                    break;
                case "ABBBB":
                    returnValue = "q";
                    break;
                case "BAAAA":
                    returnValue = "r";
                    break;
                case "BAAAB":
                    returnValue = "s";
                    break;
                case "BAABA":
                    returnValue = "t";
                    break;
                case "BAABB":
                    returnValue = "u/v";
                    break;
                case "BABAA":
                    returnValue = "w";
                    break;
                case "BABAB":
                    returnValue = "x";
                    break;
                case "BABBA":
                    returnValue = "y";
                    break;
                case "BABBB":
                    returnValue = "z";
                    break;
            }

            return returnValue;
        }

        /// <summary>
        /// 针对小写培根字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string SelectValueL(string str)
        {
            string returnValue = string.Empty;
            switch (str)
            {
                case "aaaaa":
                    returnValue = "A";
                    break;
                case "aaaab":
                    returnValue = "B";
                    break;
                case "aaaba":
                    returnValue = "C";
                    break;
                case "aaabb":
                    returnValue = "D";
                    break;
                case "aabaa":
                    returnValue = "E";
                    break;
                case "aabab":
                    returnValue = "F";
                    break;
                case "aabba":
                    returnValue = "G";
                    break;
                case "aabbb":
                    returnValue = "H";
                    break;
                case "abaaa":
                    returnValue = "I";
                    break;
                case "abaab":
                    returnValue = "J";
                    break;
                case "ababa":
                    returnValue = "K";
                    break;
                case "ababb":
                    returnValue = "L";
                    break;
                case "abbaa":
                    returnValue = "M";
                    break;
                case "abbab":
                    returnValue = "N";
                    break;
                case "abbba":
                    returnValue = "O";
                    break;
                case "abbbb":
                    returnValue = "P";
                    break;
                case "baaaa":
                    returnValue = "Q";
                    break;
                case "baaab":
                    returnValue = "R";
                    break;
                case "baaba":
                    returnValue = "S";
                    break;
                case "baabb":
                    returnValue = "T";
                    break;
                case "babaa":
                    returnValue = "U";
                    break;
                case "babab":
                    returnValue = "V";
                    break;
                case "babba":
                    returnValue = "W";
                    break;
                case "babbb":
                    returnValue = "X";
                    break;
                case "bbaaa":
                    returnValue = "Y";
                    break;
                case "bbaab":
                    returnValue = "Z";
                    break;
            }

            return returnValue;
        }
    }
}
